package routers

import (
	"context"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/route"
	"hertz/api"
	"hertz/pojo"
	myUtils "hertz/utils"
)

func loadRelation(h *route.RouterGroup) {
	relation := h.Group("/relation")
	{
		relation.POST("/action/", updateRelation)
		relation.GET("/follow/list/", getUserFollowList)
		relation.GET("/follower/list/", getUserFollowerList)
		relation.GET("/friend/list/", getUserFriendList)
	}
}

func updateRelation(c context.Context, ctx *app.RequestContext) {
	relation := myUtils.GetParamRelation(ctx)

	// 鉴权
	user, err := userProvider.Authenticate(c, &api.Token{
		Token:     relation.Token,
		Operation: "UPDATE_RELATION",
	})
	if err != nil {
		FAIL(ctx, err)
		return
	}

	if relation.ActionType == "1" {
		_, err := relationProvider.Follow(c, &api.UserRelation{
			UserAId: user.GetId(),
			UserBId: transInt64(relation.UserId),
		})
		if err != nil {
			FAIL(ctx, err)
			return
		}

		OK(ctx, nil)
	} else {
		_, err := relationProvider.UnFollow(c, &api.UserRelation{
			UserAId: user.GetId(),
			UserBId: transInt64(relation.UserId),
		})
		if err != nil {
			FAIL(ctx, err)
			return
		}

		OK(ctx, nil)
	}
}

func getUserFollowList(c context.Context, ctx *app.RequestContext) {
	relation := myUtils.GetParamRelation(ctx)

	// 鉴权
	user, err := userProvider.Authenticate(c, &api.Token{
		Token:     relation.Token,
		Operation: "GET_FOLLOW_LIST",
	})
	if err != nil {
		FAIL(ctx, err)
		return
	}

	follow, err := relationProvider.FindFollow(c, &api.UserRelation{UserAId: user.GetId()})
	if err != nil {
		FAIL(ctx, err)
		return
	}

	var list []pojo.User
	for _, value := range follow.GetList() {
		list = append(list, transUser(user, &api.User{Id: value}))
	}
	OK(ctx, utils.H{"user_list": list})
}

func getUserFollowerList(c context.Context, ctx *app.RequestContext) {
	relation := myUtils.GetParamRelation(ctx)

	// 鉴权
	user, err := userProvider.Authenticate(c, &api.Token{
		Token:     relation.Token,
		Operation: "GET_FOLLOWER_LIST",
	})
	if err != nil {
		FAIL(ctx, err)
		return
	}

	follow, err := relationProvider.FindFollower(c, &api.UserRelation{UserAId: user.GetId()})
	if err != nil {
		FAIL(ctx, err)
		return
	}

	var list []pojo.User
	for _, value := range follow.GetList() {
		list = append(list, transUser(user, &api.User{Id: value}))
	}
	OK(ctx, utils.H{"user_list": list})
}

func getUserFriendList(c context.Context, ctx *app.RequestContext) {
	relation := myUtils.GetParamRelation(ctx)

	// 鉴权
	user, err := userProvider.Authenticate(c, &api.Token{
		Token:     relation.Token,
		Operation: "GET_FRIEND_LIST",
	})
	if err != nil {
		FAIL(ctx, err)
		return
	}

	follow, err := relationProvider.FindFriend(c, &api.UserRelation{UserAId: user.GetId()})
	if err != nil {
		FAIL(ctx, err)
		return
	}

	var list []pojo.User
	for _, value := range follow.GetList() {
		list = append(list, transUser(user, &api.User{Id: value}))
	}
	OK(ctx, utils.H{"user_list": list})
}
